package com.luo.stock.service.impl;

import com.google.common.collect.Lists;
import com.luo.stock.service.StockTimerTaskService;
import com.luohai.stock.constant.ParseType;
import com.luohai.stock.mapper.*;
import com.luohai.stock.pojo.entity.StockBlockRtInfo;
import com.luohai.stock.pojo.entity.StockMarketIndexInfo;
import com.luohai.stock.pojo.entity.StockOuterMarketIndexInfo;
import com.luohai.stock.pojo.entity.StockRtInfo;
import com.luohai.stock.pojo.domain.StockInfoConfig;
import com.luohai.stock.utils.IdWorker;
import com.luohai.stock.utils.ParserStockInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author 奥观海
 * @Date 2024/9/19
 */
@Service
@Slf4j
public class StockTimerTaskServiceImpl implements StockTimerTaskService {
    @Autowired
    private StockInfoConfig stockInfoConfig;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StockMarketIndexInfoMapper stockMarketIndexInfoMapper;
    @Autowired
    private StockBusinessMapper stockBusinessMapper;
    @Autowired
    private StockRtInfoMapper stockRtInfoMapper;
    @Autowired
    private ParserStockInfoUtil parserStockInfoUtil;
    @Autowired
    private StockBlockRtInfoMapper stockBlockRtInfoMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private StockOuterMarketIndexInfoMapper stockOuterMarketIndexInfoMapper;
    @Override
    public void getInnerMarketInfo() {
        String url=stockInfoConfig.getMarketUrl() + String.join(",",stockInfoConfig.getInner());
        //2.调用restTemplate采集数据
        //2.1 组装请求头
        HttpHeaders headers = new HttpHeaders();
        //必须填写，否则数据采集不到
        headers.add("Referer","https://finance.sina.com.cn/stock/");
        headers.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
        //2.2 组装请求对象
        HttpEntity<Object> entity = new HttpEntity<>(headers);
        //2.3 resetTemplate发起请求
        String resString = restTemplate.postForObject(url, entity, String.class);
        List<StockMarketIndexInfo> list = parserStockInfoUtil.parser4StockOrMarketInfo(resString, ParseType.INNER);
        log.info("国内大盘数据",list);
        Integer integer = stockMarketIndexInfoMapper.addAll(list);
        if(integer>0){
            rabbitTemplate.convertAndSend("stockExchange","innerMarket.key",new Date());
            log.info("插入成功");
        }else {
            log.info("插入失败");
        }
    }

    @Override
    public void getOuterMarketInfo() {
        String url=stockInfoConfig.getMarketUrl() + String.join(",",stockInfoConfig.getOuter());
        //2.调用restTemplate采集数据
        //2.1 组装请求头
        HttpHeaders headers = new HttpHeaders();
        //必须填写，否则数据采集不到
        headers.add("Referer","https://finance.sina.com.cn/stock/");
        headers.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
        //2.2 组装请求对象
        HttpEntity<Object> entity = new HttpEntity<>(headers);
        //2.3 resetTemplate发起请求
        String resString = restTemplate.postForObject(url, entity, String.class);
        System.out.println(resString);
        List<StockOuterMarketIndexInfo> list = parserStockInfoUtil.parser4StockOrMarketInfo(resString, ParseType.OUTER);
        log.info("国外大盘数据",list);
        Integer integer =stockOuterMarketIndexInfoMapper.addOuterMarketInfo(list);
        if(integer>0){
        //    rabbitTemplate.convertAndSend("stockExchange","innerMarket.key",new Date());
            rabbitTemplate.convertAndSend("stockExchange","outerMarket.key",new Date());
            log.info("插入成功");
        }else {
            log.info("插入失败");
        }
    }

    @Override
    public void getInnerStockInfo() {
        List<String> codes=stockBusinessMapper.getCodeAll();
        codes=codes.stream().map(id->id.startsWith("6")?"sh" + id : "sz" + id).collect(Collectors.toList());
        //2.调用restTemplate采集数据
        //2.1 组装请求头
        HttpHeaders headers = new HttpHeaders();
        //必须填写，否则数据采集不到
        headers.add("Referer","https://finance.sina.com.cn/stock/");
        headers.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
        //2.2 组装请求对象
        HttpEntity<Object> entity = new HttpEntity<>(headers);
        //2.3 resetTemplate发起请求

        Lists.partition(codes,15).forEach(ids->{
            threadPoolTaskExecutor.execute(()->{
           String url=stockInfoConfig.getMarketUrl() + String.join(",",ids);
            String resString = restTemplate.postForObject(url, entity, String.class);
            List<StockRtInfo> stocklist= parserStockInfoUtil.parser4StockOrMarketInfo(resString, ParseType.ASHARE);
            Integer count=stockRtInfoMapper.addStockAll(stocklist);
            log.info("个股数据");
            if(count>0){
                log.info("插入成功");
            }else {
                log.info("插入失败");
            }
            });
        });
        rabbitTemplate.convertAndSend("stockExchange","innerStock.key",new Date());
    }

    @Override
    public void getInnerStockBlockRtInfo() {
        String url=stockInfoConfig.getBlockUrl();
        //2.调用restTemplate采集数据
        //2.1 组装请求头
        HttpHeaders headers = new HttpHeaders();
        //必须填写，否则数据采集不到
        headers.add("Referer","https://finance.sina.com.cn/stock/");
        headers.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
        //2.2 组装请求对象
        HttpEntity<Object> entity = new HttpEntity<>(headers);
        //2.3 resetTemplate发起请求
        String  stockStr= restTemplate.postForObject(url, entity, String.class);
        log.info("板块数据");
        List<StockBlockRtInfo> collect= parserStockInfoUtil.parse4StockBlock(stockStr);
         Lists.partition(collect,20).forEach(ids->{
             threadPoolTaskExecutor.execute(()->{
                     Integer count= stockBlockRtInfoMapper.addBlockAll(ids);
             if(count>0){
                 log.info("插入成功");
             }else {
                 log.info("插入失败");
             }
             });

                 }
         );
        rabbitTemplate.convertAndSend("stockExchange","innerStockBlock.key",new Date());

    }
}
